/*********************************************************************************
* Copyright (c) zhujinyang.
* All rights reserved.
*
* \file
*
* fifo implement, Multithread safety, Support multiple producers and consumers.
* Also supports interrupts
* 
*********************************************************************************/
#ifndef  CC_FIFO_H
#define  CC_FIFO_H





/**********************************************************************************
* fifo struct define
**********************************************************************************/
typedef struct {
    INT32U       in;
    INT32U       out;
    INT32U       size;
    INT32U       used_in;
    INT32U       used_out;
    INT8U       *buf;
} CC_FIFO_T;




/**********************************************************************************
* \brief:       create a fifo
* \param[in]:   fifo:    the pointer fifo
* \param[in]:   buf:     buffer pointer
* \param[in]:   size:    the buffer size
* \return:      none
**********************************************************************************/
void cc_fifo_create(CC_FIFO_T *fifo, INT8U *buf, INT32U size);


/**********************************************************************************
* \brief:       destory fifo
* \param[in]:   fifo:    the pointer fifo
* \return:      none
**********************************************************************************/
void cc_fifo_destory(CC_FIFO_T *fifo);

/**********************************************************************************
* \brief:       write data to fifo
* \param[in]:   fifo:    the pointer fifo
* \param[in]:   data:    buffer pointer
* \param[in]:   dlen:    the buffer length
* \return:      success : Actual write length, 0: Insufficient cache
**********************************************************************************/
INT32U cc_fifo_writedata(CC_FIFO_T *fifo, INT8U *data, INT32U dlen);


/**********************************************************************************
* \brief:       wtite a byte to fifo
* \param[in]:   fifo:    the pointer fifo
* \param[in]:   byte:    write byte
* \return:      success : Actual write length, 0: Insufficient cache
**********************************************************************************/
INT32U cc_fifo_writebyte(CC_FIFO_T *fifo, INT8U byte);


/**********************************************************************************
* \brief:       read data form fifo
* \param[in]:   fifo:    the pointer fifo
* \param[out]:  data:    receive buffer pointer
* \param[in]:   dlen:    receive buffer length
* \return:      success : Actual reception length, 0: no reception data
**********************************************************************************/
INT32U cc_fifo_readdata(CC_FIFO_T *fifo, INT8U *data, INT32U dlen);

/**********************************************************************************
* \brief:       read a byte form fifo
* \param[in]:   fifo:    the pointer fifo
* \return:      < 0: failure
**********************************************************************************/
INT32S cc_fifo_readbyte(CC_FIFO_T *fifo);



/**********************************************************************************
* \brief:       return fifo left bytes
* \param[in]:   fifo:    the pointer fifo
* \param[in]:   producer_or_consumer:    0: producer, 1: consumer
* \return:      the left bytes of fifo
**********************************************************************************/
INT32U cc_fifo_left(CC_FIFO_T *fifo,  INT32U producer_or_consumer);




/**********************************************************************************
* \brief:       return fifo used bytes
* \param[in]:   fifo:    the pointer fifo
* \param[in]:   producer_or_consumer:    0: producer, 1: consumer
* \return:      the used bytes of fifo
**********************************************************************************/
INT32U cc_fifo_used(CC_FIFO_T *fifo,  INT32U producer_or_consumer);



/**********************************************************************************
* \brief:       return fifo size
* \param[in]:   fifo:    the pointer fifo
* \return:      the size of fifo
**********************************************************************************/
INT32U cc_fifo_size(CC_FIFO_T *fifo);







#endif




